<template>
    <el-container class="home_container">
<!--      头部区域-->
      <el-header>
        <h1>电商后台管理系统</h1>
        <el-button type="info" @click="layout">退出</el-button>
      </el-header>
      <el-container>
<!--        左侧导航栏-->
        <el-aside :width="isCollapse ? '64px' : '200px'">
<!--          导航栏-->
<!--         折叠按钮 -->
          <div class="toggle_button" @click="toggle">|||</div>
          <el-menu
            :default-active="activePath"
            router
            :collapse="isCollapse"
            :collapse-transition="false"
            unique-opened
            background-color="#333744"
            text-color="#fff"
            active-text-color="#3f95ee">
<!--            一级菜单-->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
<!--              一级菜单文本-->
              <template slot="title">
              <i :class="iconsObj[item.id]"/>
              <span>{{item.authName}}</span>
            </template>
<!--              二级菜单-->
              <el-menu-item
                :index="'/' + subItem.path + ''"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="savePath('/' + subItem.path + '')">
                <template slot="title">
                  <i class="el-icon-menu"/>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      // 左侧菜单列表数据
      menuList: [],
      // 定义一个字体图标对象
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    layout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单列表
    async getMenuList () {
      const { data: res } = await this.$http.get(`menus`)
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 折叠函数
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    // 点击保存激活链接
    savePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped lang="less">
  .home_container{
    height: 100%;
    .el-header{
      height: 60px;
      background-color: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      h1{
        color: #fff;
        font-size: 26px;
        font-weight: normal;
      }
    }
    .el-aside{
      background-color: #333744;
      .iconfont{
        margin-right: 10px;
      }
      .el-menu{
        border-right: none;
      }
      .toggle_button{
        color: #fff;
        text-align: center;
        line-height: 24px;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
    }
    .el-main{
      background-color: #eaedf1;
    }
  }
</style>
